package com.xiyu.service.service.mailbox;

import com.xiyu.service.vo.mailbox.mailSignature.MailSignatureSingleGetAccountSignatureInput;
import com.xiyu.service.model.mailbox.signature.MailboxAccountSignature;
import com.xiyu.service.vo.mailbox.mailSignature.MailSignatureSaveAccountSignatureInput;
import com.xiyu.service.convert.mailbox.MailSignatureConvert;
import com.xiyu.service.model.mailbox.signature.MailboxMailSignatureDraft;
import com.xiyu.service.repository.mailbox.signature.MailboxAccountSignatureRepository;
import com.xiyu.service.vo.mailbox.mailSignature.MailSignatureListAccountSignatureOutput;
import com.xiyu.service.model.mailbox.signature.MailboxMailSignature;
import com.xiyu.service.repository.mailbox.signature.MailboxMailSignatureRepository;
import com.xiyu.service.vo.mailbox.mailSignature.MailSignatureUpdateSignatureInput;
import com.xiyu.service.vo.mailbox.mailSignature.MailSignatureCreateSignatureInput;
import com.xiyu.service.vo.mailbox.mailSignature.MailSignatureListOutput;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import jakarta.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static com.xiyu.service.framework.exception.util.ServiceExceptionUtil.exception;
import static com.xiyu.service.errorCode.mailbox.MailSignatureErrorCode.*;
import static com.xiyu.service.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * 邮箱签名 Service 实现类
 */
@Service
@Validated
public class MailSignatureServiceImpl implements MailSignatureService {

    @Resource
    private MailboxMailSignatureRepository mailboxMailSignatureRepository;

    @Resource
    private MailboxAccountSignatureRepository mailboxAccountSignatureRepository;


    @Override
    public List<MailSignatureListOutput> list() {
        return MailSignatureConvert.INSTANCE.listOutputConvert(mailboxMailSignatureRepository.findByOwnerId(getLoginUserId()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createSignature(MailSignatureCreateSignatureInput inputVO) {
        Optional<MailboxMailSignature> optionalExistSignature = mailboxMailSignatureRepository.findFirstByOwnerIdAndName(getLoginUserId(), inputVO.getName());
        if(optionalExistSignature.isPresent()){
            throw exception(MAILBOX_MAIL_SIGNATURE_DUPLICATE);
        }
        MailboxMailSignature newSignature = MailboxMailSignatureDraft.$.produce(draft -> draft.setName(inputVO.getName()).setContent(inputVO.getContent()).setOwnerId(getLoginUserId()));
        return mailboxMailSignatureRepository.insert(newSignature).id();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateSignature(MailSignatureUpdateSignatureInput inputVO) {
        Optional<MailboxMailSignature> optionalOldMailboxMailSignature = mailboxMailSignatureRepository.findById(inputVO.getId());
        if(optionalOldMailboxMailSignature.isEmpty()){
            throw exception(MAILBOX_MAIL_SIGNATURE_NOT_EXIST);
        }
        Optional<MailboxMailSignature> optionalExistSignature = mailboxMailSignatureRepository.findFirstByOwnerIdAndName(getLoginUserId(), inputVO.getName());
        if(optionalExistSignature.isPresent() && optionalExistSignature.get().id() != inputVO.getId()){
            throw exception(MAILBOX_MAIL_SIGNATURE_DUPLICATE);
        }
        inputVO.setOwnerId(getLoginUserId());
        if(inputVO.getContent() == null){
            mailboxMailSignatureRepository.updateName(inputVO.getId(), inputVO.getName());
        }else{
            mailboxMailSignatureRepository.updateContent(inputVO.getId(), inputVO.getContent());
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deletedSignature(Long id) {
        Optional<MailboxMailSignature> optionalOldMailboxMailSignature = mailboxMailSignatureRepository.findById(id);
        if(optionalOldMailboxMailSignature.isEmpty()){
            throw exception(MAILBOX_MAIL_SIGNATURE_NOT_EXIST);
        }
        mailboxAccountSignatureRepository.updateByDeleteSignatureId(getLoginUserId(), id);
        mailboxMailSignatureRepository.deleteById(id);
        return true;
    }

    @Override
    public List<MailSignatureListAccountSignatureOutput> listAccountSignature() {
        return MailSignatureConvert.INSTANCE.listAccountSignatureOutputConvert(mailboxAccountSignatureRepository.findByOwnerId(getLoginUserId()));
    }

    @Override
    public Boolean saveAccountSignature(List<MailSignatureSaveAccountSignatureInput> inputVO) {
        List<MailboxAccountSignature> mailboxAccountSignatures = MailSignatureConvert.INSTANCE.saveAccountSignatureInputConvert(inputVO);
        mailboxAccountSignatureRepository.saveAll(mailboxAccountSignatures);
        return null;
    }

    @Override
    public String singleGetAccountSignature(MailSignatureSingleGetAccountSignatureInput inputVO) {
        MailboxAccountSignature accountSignature = mailboxAccountSignatureRepository.findByAccountId(inputVO.getAccountId()).orElse(null);
        if(accountSignature == null){
            return "";
        }
        if(inputVO.getType().equals(0)){
            if(accountSignature.newMailSignatureId() == null || Objects.equals(accountSignature.newMailSignatureId(), 0L))
                return "";
            else
                return Objects.requireNonNull(accountSignature.newMailSignature()).content();
        }
        else{
            if(accountSignature.replyMailSignatureId() == null || Objects.equals(accountSignature.replyMailSignatureId(), 0L))
                return "";
            else
                return Objects.requireNonNull(accountSignature.replyMailSignature()).content();
        }

    }

}
